home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / POSTSCPT / GSVIEW / SRC / GVWDISP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-17  |  8.0 KB  |  284 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvwdisp.c */
  19. /* Display GSview routines for Windows */
  20. #include "gvwin.h"
  21.  
  22. /* handle messages while we are waiting */
  23. void
  24. do_message(void)
  25. {
  26.     MSG msg;
  27.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  28.     if ((hDlgModeless == 0) || !IsDialogMessage(hDlgModeless, &msg)) {
  29.             TranslateMessage(&msg);
  30.             DispatchMessage(&msg);
  31.         }
  32.     }
  33. }
  34.  
  35. /* run Ghostscript for previewing document */
  36. /* return TRUE if ok, FALSE if error */
  37. BOOL
  38. gs_open(void)
  39. {
  40. #ifdef WIN32
  41. char command[256];
  42. #else
  43. char command[512];
  44. #endif
  45. FILE *optfile;
  46. int depth;
  47.     /* return if already open */
  48.     if ((gsprog.valid) && IsWindow(hwndimgchild))
  49.         return TRUE;
  50.  
  51.     display.do_endfile = FALSE;
  52.     display.do_resize = FALSE;
  53.     zoom = FALSE;
  54.     pipeinit();        /* so we wait for first request */
  55.     gs_size();
  56. #ifdef OLD
  57.     sprintf(command,"\042%s\042 %s -I\042%s\042 -dBitsPerPixel=%d %s -r%gx%g -g%ux%u -sGSVIEW=%u -",
  58.         option.gsexe, option.gsother, option.gsinclude,
  59.         (option.depth ? option.depth : display.bitcount*display.planes),
  60.         option.safer ? "-dSAFER" : "", 
  61.         option.xdpi, option.ydpi, 
  62.                 display.width, display.height, (unsigned int)hwndimg);
  63. #else
  64.     /* new version uses temporary file to reduce command line length */
  65.     /* avoids setting FIXEDMEDIA and does sanity check on BitsPerPixel */
  66.  
  67.     /* restrict depth to values supported by Ghostscript */
  68.     depth = display.planes * display.bitcount;
  69.     if (depth >= 24)
  70.         depth = 24;
  71.     else if (depth >=15)
  72.         depth = 16;
  73.     else if (depth >=8)
  74.         depth = 8;
  75.     else if (depth >=4)
  76.         depth = 4;
  77.     else 
  78.         depth = 1;
  79.  
  80.     if ((display.optname[0] != '\0') && !debug)
  81.         unlink(display.optname);
  82.     display.optname[0] = '\0';
  83.     if ( (optfile = gp_open_scratch_file(szScratch, display.optname, "w")) == (FILE *)NULL) {
  84.         play_sound(SOUND_ERROR);
  85.         return FALSE;
  86.     }
  87.  
  88.     if (option.gsversion == IDM_GS351)
  89.         fprintf(optfile, "-I\042%s\042\n", option.gsinclude);
  90.     else
  91.         fprintf(optfile, "-I%s\n", option.gsinclude);
  92.     if (option.safer)
  93.         fprintf(optfile, "-dSAFER\n");
  94.     fprintf(optfile, "-dBitsPerPixel=%d\n", option.depth ? option.depth : depth);
  95.     fprintf(optfile, "-dDEVICEXRESOLUTION=%g\n", (double)option.xdpi);
  96.     fprintf(optfile, "-dDEVICEYRESOLUTION=%g\n", (double)option.ydpi);
  97.     fprintf(optfile, "-dDEVICEWIDTH=%u\n", display.width);
  98.     fprintf(optfile, "-dDEVICEHEIGHT=%u\n", display.height);
  99.     fclose(optfile);
  100.     sprintf(command,"%s %s @%s -sGSVIEW=%u -", option.gsexe, 
  101.         option.gsother, display.optname, (unsigned int)hwndimg);
  102. #endif
  103.  
  104.     if ( ((strlen(command) > 126) && !(is_winnt || is_win95))
  105.       ||  (strlen(command) > 256) ) {
  106.         display.do_display = FALSE;
  107.         gserror(IDS_TOOLONG, command, MB_ICONSTOP, SOUND_ERROR);
  108.         return FALSE;
  109.     }
  110.     info_wait(IDS_WAITGSOPEN);
  111.     gsprog.hinst = (HINSTANCE)WinExec(command, SW_SHOWMINNOACTIVE);
  112.  
  113. #ifdef __WIN32__
  114.     if ( (is_win95 && ((int)gsprog.hinst < HINSTANCE_ERROR))
  115.       || (is_winnt && ((int)gsprog.hinst < HINSTANCE_ERROR))
  116.           || (gsprog.hinst == NULL) ) {    /* Win32s WinExec returned buggy value */
  117. #else
  118.     if (gsprog.hinst < HINSTANCE_ERROR) {
  119. #endif
  120.         info_wait(IDS_NOWAIT);
  121.         display.do_display = FALSE;
  122.         gserror(IDS_CANNOTRUN, command, MB_ICONSTOP, SOUND_ERROR);
  123.         load_string(IDS_TOPICINSTALL, szHelpTopic, sizeof(szHelpTopic));
  124.         get_help();
  125.         return FALSE;
  126.     }
  127.     {   /* TEMPORARY KLUDGE */
  128.     int i;
  129.     for (i=0; i<10; i++)
  130.         do_message();    /* wait for gswin to establish with debugger running */
  131.     }
  132. #ifdef NOTUSED
  133. #endif
  134.     if (hwndtext == (HWND)NULL) {
  135.         /* we are running an incompatible version of Ghostscript */
  136.         hwndtext = FindWindow("BCEasyWin","Ghostscript");
  137.         if (hwndtext) {
  138.             SendMessage(hwndtext, WM_CHAR, 'q', 1L);
  139.             SendMessage(hwndtext, WM_CHAR, 'u', 1L);
  140.             SendMessage(hwndtext, WM_CHAR, 'i', 1L);
  141.             SendMessage(hwndtext, WM_CHAR, 't', 1L);
  142.             SendMessage(hwndtext, WM_CHAR, '\r', 1L);
  143.         }
  144.         hwndtext = (HWND)NULL;
  145.         hwndimgchild = (HWND)NULL;
  146.         gsprog.hinst = (HINSTANCE)NULL;
  147.         clear_timer();
  148.         info_wait(IDS_NOWAIT);
  149.         gserror(IDS_WRONGGS, NULL, MB_ICONSTOP, SOUND_ERROR);
  150.         return FALSE;
  151.     }
  152.  
  153.     /* wait for gswin to initialise */
  154.     if (set_timer(CLOSE_TIMEOUT))
  155.         EnableWindow(hwndimg, FALSE);
  156.     while (!is_pipe_done()&&  !bTimeout)
  157.         do_message();    /* wait for pipe data request from gswin */
  158.     clear_timer();
  159.     EnableWindow(hwndimg, TRUE);
  160.  
  161.     display.saved = FALSE;
  162.     display.page = FALSE;
  163.     display.sync = FALSE;
  164.     gsprog.input = pipeopen();    /* open pipe to gswin */
  165.     if (gsprog.input == (FILE *)NULL) {
  166.         gs_close();
  167.         return FALSE;
  168.     }
  169.     gsprog.valid = TRUE;
  170.     BringWindowToTop(hwndimg);
  171.     SetFocus(hwndimg);    /* kludge: without this desktop gets focus */
  172.     return TRUE;
  173. }
  174.  
  175. /* close Ghostscript */
  176. BOOL
  177. gs_close(void)
  178. {
  179. BOOL force = FALSE;
  180.     if (!gsprog.valid)
  181.         return TRUE;
  182.  
  183.     if (doc == (PSDOC *)NULL) {
  184.         /* we don't know how many pages remain so we must force an exit */
  185.         if (!is_pipe_done())
  186.         force = TRUE;
  187.     }
  188.     else {
  189.         if (display.page)
  190.         next_page();
  191.     }
  192.  
  193.     if (!force) {
  194.         /* try to close Ghostscript cleanly */
  195.         pipeclose();
  196.         if (set_timer(CLOSE_TIMEOUT))
  197.         EnableWindow(hwndimg, FALSE);
  198. #ifdef __WIN32__
  199.         {   /* TEMPORARY KLUDGE */
  200.         int i;
  201.         for (i=0; i<10; i++) {
  202.             do_message();    /* wait for gswin to close */
  203.             Sleep(50);
  204.         }
  205.         }
  206. #else
  207.         while (GetModuleUsage(gsprog.hinst) &&  !bTimeout)
  208.         do_message();    /* wait for gswin to close */
  209. #endif
  210.         clear_timer();
  211.         EnableWindow(hwndimg, TRUE);
  212.     }
  213.     do_message();
  214.  
  215.     /* if still there try killing it a using a brute force method */
  216.     if (IsWindow(hwndtext)) {
  217.         if (is_win31) {
  218.             SendMessage(hwndtext, WM_CLOSE, 0, 0L);
  219.             if (IsWindow(hwndtext))
  220.             SendMessage(hwndtext, WM_CLOSE, 0, 0L);
  221.         }
  222.         else {
  223.             /* Windows 3.0 hangs if we use SendMessage */
  224.             PostMessage(hwndtext, WM_CLOSE, 0, 0L);
  225.             do_message();
  226.         }
  227.     }
  228.  
  229.     do_message();
  230.     gsprog.hinst = (HINSTANCE)NULL;
  231.     hwndimgchild = (HWND)NULL;
  232.     hwndtext = (HWND)NULL;
  233.     bitmap_scrollx = bitmap_scrolly = 0;
  234.     display.saved = FALSE;
  235.     display.epsf_clipped = FALSE;
  236.     display.page = FALSE;
  237.     display.sync = FALSE;
  238.     pipeclose();
  239.  
  240.     if ((display.optname[0] != '\0') && !debug)
  241.         unlink(display.optname);
  242.     display.optname[0] = '\0';
  243.     return TRUE;
  244. }
  245.  
  246. /* send a NEXT_PAGE message to Ghostscript */
  247. void
  248. next_page(void)
  249. {
  250. int i;
  251.     if (hwndimgchild && IsWindow(hwndimgchild)) {
  252.         SendMessage(hwndimgchild, WM_GSVIEW, NEXT_PAGE, 0L);
  253.         display.page = FALSE;
  254.     }
  255.     do_message();    /* wait for Ghostscript to process message */
  256.     for (i=0; i<32; i++) {
  257.        /* Wait a bit for pipe contents after showpage to be read */
  258.        do_message();
  259.        if (is_pipe_done())
  260.         break;
  261.     }
  262. }
  263.  
  264.  
  265.  
  266. /* return TRUE if file length or modification time changed */
  267. BOOL
  268. psfile_changed(void)
  269. {
  270. struct ftime thisftime;
  271. long thisflength;
  272.     getftime(fileno(psfile.file), &thisftime);
  273.     thisflength = filelength(fileno(psfile.file));
  274.     return ( (thisflength != psfile.length) ||
  275.         memcmp(&thisftime, &psfile.datetime, sizeof(thisftime)) );
  276. }
  277.  
  278. void
  279. psfile_savestat(void)
  280. {
  281.     getftime(fileno(psfile.file), &psfile.datetime);
  282.     psfile.length = filelength(fileno(psfile.file));
  283. }
  284.